package com.hhit.collaborative_learning.management;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.UserDao;
import com.hhit.collaborative_learning.dao.impl.PublicDaoImplMySql;
import com.hhit.collaborative_learning.dao.impl.UserDaoImpl;
import com.hhit.collaborative_learning.entity.User;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.mysql.jdbc.StringUtils;

public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("id");
		String password = request.getParameter("password");
		String link = request.getParameter("link");
		link = URLDecoder.decode(link, "utf-8");
		PublicDaoImplMySql dao = new PublicDaoImplMySql();
		User user = dao.validateLogin(username, password);
		link = StringUtils.isNullOrEmpty(link) ? "learning/learning.jsp" : link;
		if (user != null) {
			String uRight = getUserBBSRight(user.userId);
			// request.getSession().setAttribute("cl_userInfo", user);
			Cookie userId = new Cookie("userId", user.userId + "");
			userId.setPath("/");
			response.addCookie(userId);

			Cookie userIcon = new Cookie("userIcon", user.userIcon);
			userId.setPath("/");
			response.addCookie(userIcon);

			Cookie userNickName = new Cookie("userNickName", URLEncoder.encode(
					user.userNickName, "utf-8"));
			userId.setPath("/");
			response.addCookie(userNickName);

			Cookie digged = new Cookie("digged", user.digged);
			digged.setPath("/");
			response.addCookie(digged);

			Cookie userRight = new Cookie("uRight",uRight );
			digged.setPath("/");
			response.addCookie(userRight);

			response.sendRedirect(link);
		} else {
			request.setAttribute("msg", "用户名或密码错误！");
			request.getRequestDispatcher("/login.jsp").forward(request,
					response);
		}
	}

	private String getUserBBSRight(int id) {
		Connection con = DBAccess.getInstance().getConn();
		UserDao ud = new UserDaoImpl();
		ResultSet re = ud.queryUserRight(con, " userId=" + id);
		JSONArray uRightArray = new JSONArray();
		JSONObject jobj=new JSONObject();
		try {
			uRightArray = JsonUtil.formatRsToJsonArray(re);
			jobj.put("uarray", uRightArray.toString());
			return jobj.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} finally {
			DBAccess.getInstance().close(con, null, null);
		}
	}

}
